package com.xk857.simpleeffect.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.activity.FriendInfoActivity;
import com.xk857.simpleeffect.domain.vo.FriendListVo;
import com.xk857.simpleeffect.domain.vo.FriendVo;

import java.util.List;

public class ListFriendAdapter extends BaseQuickAdapter<FriendListVo, ListFriendAdapter.ViewHolder> {

    private final Context mContext;


    public ListFriendAdapter(Context context, List<FriendListVo> voList) {
        super(voList);
        this.mContext = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, int i, @Nullable FriendListVo friendList) {
        holder.pinyinTextView.setText(friendList.getKey());
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        holder.recyclerView.setLayoutManager(layoutManager);
        ListFriendItemAdapter adapter = new ListFriendItemAdapter(mContext, friendList.getFriendList());
        holder.recyclerView.setAdapter(adapter);
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup parent, int i) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.list_friend, parent, false));
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView pinyinTextView;
        RecyclerView recyclerView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.pinyinTextView = itemView.findViewById(R.id.pinyin_text_view);
            this.recyclerView = itemView.findViewById(R.id.recycler_view);
        }

    }

    /**
     * 子布局
     */
    static class ListFriendItemAdapter extends BaseQuickAdapter<FriendVo, ListFriendItemAdapter.ViewHolder> {

        private final Context mContext;

        public ListFriendItemAdapter(Context context, List<FriendVo> voList) {
            super(voList);
            this.mContext = context;
        }


        @Override
        protected void onBindViewHolder(@NonNull ViewHolder holder, int i, @Nullable FriendVo friend) {
            Glide.with(mContext).load(friend.getHeadImg()).into(holder.headImgView);
            holder.nickNameText.setText(friend.getNickName());
            holder.mainLayout.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, FriendInfoActivity.class);
                intent.putExtra("customerId", friend.getToId());
                intent.putExtra("headImg", friend.getHeadImg());
                intent.putExtra("nickName", friend.getNickName());
                mContext.startActivity(intent);
            });
        }

        @NonNull
        @Override
        protected ViewHolder onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup parent, int i) {
            return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.list_friend_item, parent, false));
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            ImageView headImgView;
            TextView nickNameText;
            LinearLayout mainLayout;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                this.headImgView = itemView.findViewById(R.id.head_img_view);
                this.nickNameText = itemView.findViewById(R.id.nick_name_text);
                this.mainLayout = itemView.findViewById(R.id.main_layout);
            }

        }
    }

}
